<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML Imports</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link rel="stylesheet" href="spec.css" type="text/css">
<link rel="stylesheet" href="prettify.css" type="text/css">
<link rel="stylesheet" type="text/css" href="http://www.w3.org/StyleSheets/TR/W3C-WD"/>
</head>

<body>

<div class="head">

<div class="logo">
    <a href="//www.w3.org/"><img width="72" height="48" src="//www.w3.org/Icons/w3c_home" alt="W3C"></a>
</div>

<h1>HTML Imports</h1>
<h2 id="editors-draft">W3C Working Draft 11 March 2014</h2>
<dl>
<dt>This version</dt>
    <dd><a href="http://www.w3.org/TR/2014/WD-html-imports-20140311/">http://www.w3.org/TR/2014/WD-html-imports-20140311/</a></dd>
<dt>Latest version</dt>
    <dd><a href="http://www.w3.org/TR/html-imports/">http://www.w3.org/TR/html-imports/</a></dd>
<dt>Previous version</dt>
    <dd><a href="http://www.w3.org/TR/2013/WD-html-imports-20130514/">http://www.w3.org/TR/2013/WD-html-imports-20130514/</a></dd>
<dt>Revision history</dt>
    <dd><a id="log" href="https://github.com/w3c/webcomponents/commits/gh-pages/spec/imports/">https://github.com/w3c/webcomponents/commits/gh-pages/spec/imports/</a></dd>
<dt>Participate</dt>
    <dd>Discuss on <a href="http://lists.w3.org/Archives/Public/public-webapps/">public-webapps@w3.org</a> (<a href="http://www.w3.org/2008/webapps/">Web Applications Working Group</a>)</dd>
    <dd><a href="https://www.w3.org/Bugs/Public/enter_bug.cgi?comment=&amp;blocked=20683&amp;short_desc=%5Bimports%5D%3A%20&amp;product=WebAppsWG&amp;import=import%20Model">File bugs</a> (w3.org's <a href="https://www.w3.org/Bugs/Public/">Bugzilla</a>)</dd>
<dt>Editor</dt>
    <dd class="vcard"><span class="fn">Dimitri Glazkov</span>, <span class="org">Google</span>, &lt;<a class="email" href="mailto:dglazkov@chromium.org">dglazkov@chromium.org</a>&gt;</dd>
    <dd class="vcard"><span class="fn">Hajime Morrita</span>, <span class="org">Google</span>, &lt;<a class="email" href="mailto:morrita@chromium.org">morrita@chromium.org</a>&gt;</dd>
</dl>

<p class="copyright"><a href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a> © 2014 <a href="http://www.w3.org/"><abbr title="World Wide Web Consortium">W3C</abbr></a><sup>®</sup> (<a href="http://www.csail.mit.edu/"><abbr title="Massachusetts Institute of Technology">MIT</abbr></a>, <a href="http://www.ercim.eu/"><abbr title="European Research Consortium for Informatics and Mathematics">ERCIM</abbr></a>, <a href="http://www.keio.ac.jp/">Keio</a>, <a href="http://ev.buaa.edu.cn/">Beihang</a>), All Rights Reserved. W3C <a href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>, <a href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a> and <a href="http://www.w3.org/Consortium/Legal/copyright-documents">document use</a> rules apply.</p><hr>

<h2 id="abstract">Abstract</h2>

<p>HTML Imports are a way to include and reuse HTML documents in other HTML documents.</p>

<h2 id="status">Status of This Document</h2>

<p><em>This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current <abbr title="World Wide Web Consortium">W3C</abbr> publications and the latest revision of this technical report can be found in the <a href="http://www.w3.org/TR/"><abbr title="World Wide Web Consortium">W3C</abbr> technical reports index</a> at http://www.w3.org/TR/.</em></p>

<p>This document was published by the <a href="http://www.w3.org/2008/webapps/">Web Applications Working Group</a> as an Working Draft. If you wish to make comments regarding this document, please send them to <a href="mailto:public-webapps@w3.org">public-webapps@w3.org</a> (<a href="mailto:public-webapps-request@w3.org?subject=subscribe">subscribe</a>, <a href="http://lists.w3.org/Archives/Public/public-webapps/">archives</a>) with a Subject: prefix of <code>[html-imports]</code>. All feedback is welcome.</p><p>Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.</p>

<p>This document was produced by a group operating under the <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February 2004 <abbr title="World Wide Web Consortium">W3C</abbr> Patent Policy</a>. <abbr title="World Wide Web Consortium">W3C</abbr> maintains a <a href="http://www.w3.org/2004/01/pp-impl/42538/status" rel="disclosure">public list of any patent disclosures</a> made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential Claim(s)</a> must disclose the information in accordance with <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section 6 of the <abbr title="World Wide Web Consortium">W3C</abbr> Patent Policy</a>.</p>

</div>

<section class="toc">
<h2 id="toc">Table of Contents</h2>
<ol>
    <li><a href="#about">About this Document</a>
    <li><a href="#dependencies">Dependencies</a>
    <li><a href="#terminology">Terminology</a></li>
    <li><a href="#link-type-import">Link Type "<code>import</code>"</a>
    <li><a href="#interface-import">Extensions to <code>HTMLLinkElement</code> interface</a>
    <li><a href="#loading-imports">Loading Imports</a>
    <ol>
      <li><a href="#import-dependent">Import Dependent</a>
      <li><a href="#requesting-import">Requesting Import</a>
      <li><a href="#fetching-import">Fetching Import</a>
      <li><a href="#imports-and-csp">Imports and Content Security Policy</a>
    </ol>
    <li><a href="#parsing-imports">Parsing Imports</a>
    <ol>
        <li><a href="#additions-to-prepare-a-script-algorithm">Additions to Prepare A Script Algorithm</a>
        <li><a href="#additions-to-tree-construction-algorithm">Additions to Tree Construction Algorithm</a>
        <li><a href="#additions-to-parsing-xhtml-documents">Additions to Parsing XHTML Documents</a>
        <li><a href="#additions-to-document-write">Additions to document.write() method</a>
    </ol>
    <li><a href="#scripts-imports">Scripting in Imports</a>
    <ol>
       <li><a href="#additions-to-script-enabling-criteria">Additions to Script Enabling Criteria</a>
       <li><a href="#additions-to-dom-document-currentscript">Additions to document.currentScript</a>
    </ol>
    <li><a href="#style-imports">Style processing with Imports</a>
    <li><a href="#events-imports">Events in Imports</a>
    <ol>
      <li><a href="#additions-to-event-handlers">Additions to Event Handlers</a>
    </ol>
    <li><a href="#acknowledgements" class="no-number">Acknowledgements</a>
</ol>
</section>

<section class="spec">
<h2 id="about">About this Document</h2>

<p>All diagrams, examples, notes, are non-normative, as well as sections explicitly marked as non-normative. Everything else in this specification is normative.</p>

<p>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in <a href="http://dev.w3.org/2006/xbl2/#refsRFC2119">RFC2119</a>. For readability, these words do not appear in all uppercase letters in this specification.</p>

<p>Any point, at which a conforming UA must make decisions about the state or reaction to the state of the conceptual model, is captured as <a href="http://en.wikipedia.org/wiki/Algorithm">algorithm</a>. The algorithms are defined in terms of processing equivalence. The <dfn id="dfn-processing-equivalence">processing equivalence</dfn> is a constraint imposed on the algorithm implementors, requiring the output of the both UA-implemented and the specified algorithm to be exactly the same for all inputs.</p>

<h2 id="dependencies">Dependencies</h2>

<p>This document relies on the following specifications:</p>

<ul>
<!--     <li><a href="http://www.w3.org/TR/CSS2/">CSS Level 2 Revision 1</a></li>
    <li><a href="http://dev.w3.org/csswg/cssom/">CSSOM</a></li>
    <li><a href="http://www.w3.org/TR/DOM-Level-3-Events/">DOM Level 3 Events</a></li> -->
    <li><a href="http://dom.spec.whatwg.org/">DOM4 (DOM Core)</a></li>
    <li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/">HTML</a></li>
    <li><a href="http://domparsing.spec.whatwg.org/">DOM Parsing and Serialization</a></li>
    <li><a href="http://ecma-international.org/ecma-262/5.1/">ECMAScript Language Specification</a></li>
    <li><a href="http://url.spec.whatwg.org/">URL</a></li>
<!--     <li><a href="http://www.w3.org/TR/selectors/">Selectors Level 3</a></li>
    <li><a href="http://dev.w3.org/2006/webapi/selectors-api/">Selectors API Level 1</a></li>
    <li><a href="http://www.w3.org/TR/shadow-dom/">Shadow DOM</a></li>
    <li><a href="http://www.w3.org/TR/WebIDL/">Web IDL</a></li> -->
</ul>

<h2 id="terminology">Terminology</h2>

<p>HTML Imports, or just <dfn id="dfn-import">imports</dfn> from here on, are <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html">HTML documents</a> that are <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element">linked</a> as <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#external-resource-link">external resources</a> from another <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html">HTML document</a>. The document that links to an <a href="#dfn-import">import</a> is called an <dfn id="dfn-import-referrer">import referrer</dfn>. For any given <a href="#dfn-import">import</a>, an <dfn id="dfn-import-referrer-ancestor">import referrer ancestor</dfn> is its <a href="#dfn-import-referrer">import referrer</a> or any <a href="#dfn-import-referrer-ancestor">import referrer ancestor</a> of its <a href="#dfn-import-referrer">import referrer</a>.

<p>An <a href="#dfn-import-referrer">import referrer</a> which has its own <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#browsing-context">browsing context</a> is called a <dfn id="dfn-master-document">master document</dfn>. Each <a href="#dfn-import">import</a> is associated with one <a href="#dfn-master-document">master document</a>: if the <a href="#dfn-import-referrer">referrer</a> of the <a href="#dfn-import">import</a> is a <a href="#dfn-master-document">master document</a>, it is the <a href="#dfn-master-document">master document</a> of the <a href="#dfn-import">import</a>. Otherwise, the <a href="#dfn-master-document">master document</a> of the <a href="#dfn-import-referrer">import referrer</a> is the <a href="#dfn-master-document">master document</a> of the <a href="#dfn-import">import</a>.</p>

<div class="fixme">
Imported documents share its <code>defaultView</code> with the master document.
(See <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=23170">Bug 23170</a> and <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=22305">Bug 22305</a>.)
</div>

<p>The <a href="http://dom.spec.whatwg.org/#concept-document-url">URL</a> of an <a href="#dfn-import">import</a> is called the <dfn id="dfn-import-location">import location</dfn><!--, and the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/origin-0.html#origin">origin</a> of the <a href="#dfn-import-location">import location</a> is called <dfn id="dfn-import-origin">import origin</dfn>-->.</p>

<p>In the <a href="#dfn-import-referrer">import referrer</a>, an <a href="#dfn-import">import</a> is represented as a <a href="http://dom.spec.whatwg.org/#interface-document"><code>Document</code></a>, called the <dfn id="dfn-imported-document">imported document</dfn>. The <a href="#dfn-imported-document">imported documents</a> don't have <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#browsing-context">browsing context</a>.</p>

<p>The set of all <a href="#dfn-import">imports</a> associated with the <a href="#dfn-master-document">master document</a> forms an <dfn id="dfn-import-map">import map</dfn> of the <a href="#dfn-master-document">master document</a>. The maps stores <a href="#dfn-import">imports</a> as its items with their <a href="#dfn-import-location">import locations</a> as keys. The import map is empty at beginning. New items are added to the map as <a href="#dfn-import-fetching">import fetching algorithm</a> specifies.</p>

<h2 id="link-type-import">Link Type "<code>import</code>"</h2>

<p>To enable declaring <a href="#dfn-import">imports</a> in HTML, a new link type is added to <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#linkTypes">HTML link types</a>:</p>

<div class="monkeypatch">
<p>The <a href="#link-type-import"><code>import</code></a> keyword may be used with <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> elements. This keyword creates an <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#external-resource-link">external resource link</a> to an <a href="#dfn-import">import</a>.</p>

<p>The default type for resources given by the <a href="#link-type-import"><code>import</code></a> keyword is <code>text/html</code>.</p>

<p>The <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> element may have an <dfn id="dfn-import-async-attribute">async</dfn> attribute. The <a href="#dfn-import-async-attribute"><code>async</code></a> attribute is a <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#boolean-attributes">boolean attribute</a>.

<p>The appropriate time to <dfn id="dfn-obtaining-import"><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#concept-link-obtain">obtain</a></dfn> the resource is when the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#external-resource-link">external resource link</a> is created or when its element is <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#insert-an-element-into-a-document">inserted into a document</a>, whichever happens last.</p>

</div>

<div class="informative">
<p>The following document has one import, located at <kbd>/imports/heart.html</kbd>:</p>

<pre><code class="prettyprint">
&lt;!DOCTYPE html&gt;
&lt;html lang="en-US"&gt;
    &lt;head&gt;
        &lt;title&gt;Human Being&lt;/title&gt;
        &lt;link rel="import" href="/imports/heart.html"&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;p&gt;What is a body without a heart?&lt;/p&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>
</div>

<h2 id="interface-import">Extensions to <code>HTMLLinkElement</code> Interface</h2>

<pre><code class="prettyprint">
partial interface <dfn id="dfn-link-import">LinkImport</dfn> {
    readonly attribute <a href="http://dom.spec.whatwg.org/#interface-document">Document</a>? <a href="#dfn-html-link-element-import">import</a>;
};

<a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#htmllinkelement">HTMLLinkElement</a> implements <a href="#dfn-link-import">LinkImport</a>;
</code></pre>

<p>On getting, the <dfn id="dfn-html-link-element-import"><code>import</code></dfn> attribute <strong>must</strong> return <strong>null</strong>, if:</p>
<ul>
    <li>The <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> does not <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#represents">represent</a> an <a href="#dfn-import">import</a></li>
    <li>the user agent does not support <a href="#dfn-import">imports</a></li>
    <li>the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> element is not <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#in-a-document">in a <code>Document</code></a></li>
</ul>
<p>Otherwise, the attribute <strong>must</strong> return the <a href="#dfn-imported-document">imported document</a> for the <a href="#dfn-import">import</a>, represented by the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> element.</p>

<p>The same object <strong>must</strong> be returned each time.</p>

<div class="informative">
<p>Here's how one could access the imported document, mentioned in the previous example:</p>
<pre><code class="prettyprint">
var link = document.querySelector('link[rel=import]');
var heart = link.import;
// Access DOM of the document in /imports/heart.html
var pulse = heart.querySelector('div.pulse');
</code></pre>
</div>

<p>An <a href="#dfn-import">import</a> in the context of the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document"><code>Document</code></a> of an <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#html-parser">HTML parser</a> or <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-xhtml-syntax.html#xml-parser">XML Parser</a> is said to be <dfn id="dfn-an-import-that-is-blocking-scripts">an import that is blocking scripts</dfn> if the <a href="http://dom.spec.whatwg.org/#concept-element">element</a> was created by that <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document"><code>Document</code></a>'s parser, or and the <a href="http://dom.spec.whatwg.org/#concept-element">element</a> is a <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> of type <A href="#link-type-import"><code>import</code></a> when the <a href="http://dom.spec.whatwg.org/#concept-element">element</a> was created by the parser, and the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> is not <a href="#dfn-import-link-async">marked as async</a>, and the the import is yet to be <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#completely-loaded">completely loaded</a>, and, the last time the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#event-loop">event loop</a> has reached step 1, the <a href="http://dom.spec.whatwg.org/#concept-element">element</a> was <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#in-a-document">in that <code>Document</code></a>, and the user agent hasn't given up on that <a href="#dfn-import">import</a> yet. A user agent may give up on an <a href="#dfn-import">import</a> at any time.</p>


<div class="informative">
<p>
Giving up an import before it loads, even if the import eventually does still load, means that the script might end up operating with incorrect information. For example, if an import registers a custom element and a script relies on the availability of this element, the script will find that this element is unavailable if the user agent gives up early. Implementors have to balance the likelihood of a script using incorrect information with the performance impact of doing nothing while waiting for a slow network request to finish.
</p>
</div>

<p>A <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document"><code>Document</code></a> <dfn id="dfn-has-an-import-that-is-blocking-scripts">has an import that is blocking scripts</dfn> if there is <a href="#dfn-an-import-that-is-blocking-scripts">an import that is blocking scripts</a> in the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document"><code>Document</code></a>'s <a href="#dfn-import-dependent">import dependent</a>.
A <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document"><code>Document</code></a> <dfn id="dfn-has-no-import-that-is-blocking-scripts">has no import that is blocking scripts</dfn> if it does not <a href="#dfn-has-no-import-that-is-blocking-scripts">have an import that is blocking scripts</a> as defined in the previous paragraph.</p>

<div class="informative">
<p>The sate of "<a href="#dfn-has-an-import-that-is-blocking-scripts">has an import that is blocking scripts</a>" can change each time an existing import is completely loaded or new import loading is started. HTML parser has changes to unblock it for each of such timings.</p>
</div>

<h2 id="loading-imports">Loading Imports</h2>

<h3 id="import-dependent">Import Dependent</h3>

<p>Each document has an <dfn id="dfn-import-link-list">import link list</dfn>, each of whose item is consist of <dfn id="dfn-import-link-link">link</dfn>, the <code><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element">link</a></code> element and <dfn id="import-link-list-location">location</dfn>, a URL.
Also, the item is optionally <dfn id="dfn-import-link-list-cycle">marked as cycle</dfn>.
The list is empty at beginning and the item is added as <a href="#dfn-import-request">import request</a> altorighm specifies.</p>

<p>An <a href="#dfn-imported-document">imported document</a> has zero or more <dfn id="dfn-import-import-ancestor">import ancestors</dfn>. The <a href="#dfn-import-import-ancestor">import ancestor</a> is a document. If the <a href="#dfn-import-link-list">import link list</a> of document <var>A</var> contains an non-<a href="#dfn-import-link-list-cycle">cycle</a> item whose <a href="#import-link-list-location">location</a> points document <var>B</var>, <var>A</var> is a <a href="#dfn-import-import-ancestor">import ancestor</a> of <var>B</var>. <var>B</var> is also called the <dfn id="dfn-import-parent">import parent</dfn> of the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document"><code>Document</code></a>. The <a href="#dfn-import-import-ancestor">import ancestor</a> is transitive: If document <var>C</var> is a <a href="#dfn-import-import-ancestor">import ancestor</a> of document <var>B</var> and document <var>B</var> is a <a href="#dfn-import-import-ancestor">import ancestor</a> of document <var>A</var>, <var>C</var> is a <a href="#dfn-import-import-ancestor">import ancestor</a> of document <var>A</var>.</p>

<p>An <a href="#dfn-imported-document">imported document</a> also has one or more <dfn id="dfn-import-import-predecessor">import predecessor</dfn>. The <a href="#dfn-import-import-predecessor">import predecessor</a> is a document. If the URL of document <var>A</var> is located before the URL of document <var>B</var> in the <a href="#dfn-import-link-list">import link list</a> of <var>B</var>'s <a href="#dfn-import-parent">import parent</a>, <var>A</var> is <a href="#dfn-import-import-predecessor">import predecessor</a> of <var>B</var>. The <a href="#dfn-import-import-predecessor">import predecessor</a> is transitive. If document <var>A</var> is <a href="#dfn-import-import-predecessor">import predecessor</a> of document <var>B</var> and <var>B</var> is redecessor of document <var>C</var>, <var>A</var> is <a href="#dfn-import-import-predecessor">import predecessor</a> of <var>C</var>.</p> 

<p>The <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document"><code>Document</code></a> that is in either <a href="#dfn-import-import-ancestor">import ancestors</a> or <a href="#dfn-import-import-predecessor">import predecessors</a> of document <var>A</var>, or is linked from non-<a href="#dfn-import-link-list-cycle">cycle</a> item of <var>A</var>'s <a href="#dfn-import-link-list">import link list</a>, is the <dfn id="dfn-import-dependent">import dependent</dfn> of <var>A</var>.</p>

<div class="informative">
<p>The <a href="#dfn-import-link-list">import link list</a> and the <a href="#dfn-import-dependent">import dependent</a> constrains the order of script execution in imports. It is intend to give a deterministic order of script execution which is defined by the order of <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> element in each import.</p>

<p>The linking structure of <a href="#dfn-import-link-list">import link lists</a> forms a directed asyclic graph (DAG). Each node of the DAG is a document and its edge is a <a href="#dfn-import-link-link">link</a>. It cannot be a tree because more than one link can point same import. Any cycle is marked by the <a href="#dfn-import-request">import request</a> algorithm and excluded from dependency calculation.
The edges of each node is ordered in terms of <a href="#dfn-import-link-list">import link list</a>. The <a href="#dfn-import-import-predecessor">import predecessors</a> selection is aware of the order.</p>

<figure id="fig-import-list">
  <img src="import-link-list.png">
  <figcaption>
    An example of import link lists.
  </figcaption>
</figure>

<p>
In the figure,
</p>
<ul>
  <li>The document <var>A</var> has an <a href="#dfn-import-link-list">import link list</a> which contains a link to <var>B</var> and another link to <var>C</var>.
  <li>The document <var>D</var> has a set of <a href="#dfn-import-import-ancestor">import ancestors</a> that are <var>A</var>, <var>B</var> and <var>c</var>.
  <li>As <var>B</var> is a <a href="#dfn-import-import-ancestor">import ancestor</a> of <var>D</var>, one of <var>D</var>'s list item that points <var>B</var> is marked as <a href="#dfn-import-link-list-cycle">cycle</a>.
  <li>The <a href="#dfn-import-import-predecessor">import predecessors</a> of <var>E</var> are <var>B</var> and <var>D</var>, its <a href="#dfn-import-import-ancestor">import ancestors</a> are <var>A</var> and <var>C</var> and its <a href="#dfn-import-link-list">import link list</a> is empty.
      Thus its <a href="#dfn-import-dependent">import dependents</a> are <var>A</var>, <var>B</var>, <var>C</var> and <var>D</var>.
  <li>The document <var>D</var> has two <a href="#dfn-import-parent">import parents</a> which are <var>B</var> and <var>C</var>.
</ul>


<p>The difference between the <a href="#dfn-import-referrer">import referrer</a> and the <a href="#dfn-import-parent">import parent</a> is that <a href="#dfn-import-referrer">import referrer</a> reflects the state of the <a href="http://www.w3.org/TR/dom/#node-tree">node tree</a> and that the <a href="#dfn-import-parent">import parent</a> is built by the algorithm described in this document.</p>

</div>

<h3 id="requesting-import">Requesting Import</h3>

<p>When user agents attempt to <a href="#dfn-obtaining-import">obtain</a> a linked import, they <strong>must</strong> also run the <dfn id="dfn-import-request">import request</dfn> algorithm, which is <a href="#dfn-processing-equivalence">equivalent</a> to running these steps:</p>

<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>LINK</var>, the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> element that creates an <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#external-resource-link">external resource link</a> to the import.</dd>
<dd><var>LOCATION</var>, the <a href="http://dom.spec.whatwg.org/#concept-document-url">URL</a> of the linked resource.</dd>
</dl>
<ol>
  <li>If the <a href="#dfn-import-async-attribute">async</a> attribute of <var>LINK</var> is true, mark <var>LINK</var> as <dfn id="dfn-import-link-async">async</dfn>.
  <li>Let <var>DOCUMENT</var> be a document of <var>LINK</var>.
  <li>Let <var>LIST</var> be an <a href="#dfn-import-link-list">import link list</a> of <var>DOCUMENT</var>.
  <li>Let <var>ITEM</var> be <var>LINK</var> and <var>LOCATION</var>:</li>
  <li><ol>
    <li>If there is a <a href="#dfn-import-import-ancestor">import ancestor</a> of <var>DOCUMENT</var> whose URL is same as <var>LOCATION</var>, Mark <var>ITEM</var> as <a href="#dfn-import-link-list-cycle">cycle</a>.
    <li>Add <var>ITEM</var> at the end of <var>LIST</var>.
  </ol></li>
</ol>
</div>

<h3 id="fetching-import">Fetching Import</h3>

<p>All <a href="#dfn-import">imports</a> linked from documents that is the <a href="#dfn-master-document">master document</a> or the one in the <a href="#dfn-import-map">import map</a> <strong>must</strong> be loaded using <a href="http://fetch.spec.whatwg.org/#concept-legacy-potentially-cors-enabled-fetch">potentially CORS-enabled fetch</a> with <em>mode</em> set to "<a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/fetching-resources.html#attr-crossorigin-anonymous">Anonymous</a>".</p>

<p>When an <a href="#dfn-import">import</a> is <a href="http://fetch.spec.whatwg.org/#concept-fetch">fetched</a>, the user agent <strong>must</strong> run the <dfn id="dfn-import-fetching">import fetching</dfn> algorithm, which <strong>must</strong> be <a href="#dfn-processing-equivalence">equivalent</a> to running these steps:</p>
<div class="algorithm">
<dl>
<dt>Input</dt>
<dd><var>LINK</var>, a <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> element which makes the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#external-resource-link">external resource link</a> to the import.</dd>
<dd><var>LOCATION</var>, the <a href="#dfn-import-location">import location</a></dd>
<dt>Output</dt>
<dd><var>IMPORT</var>, the <a href="#dfn-imported-document">imported document</a>.</dd>
</dl>
<ol>
    <li>If <var>LOCATION</var> is already in the <a href="#dfn-import-map">import map</a>:</li>
    <li><ol>
      <li>Let <var>IMPORT</var> be the <a href="#dfn-imported-document">imported document</a> for <var>LOCATION</var> and <strong>stop</strong>.</li>
    </ol></li>
    <li><a href="http://fetch.spec.whatwg.org/#concept-fetch">Fetch a resource</a> from <var>LOCATION</var>, with <var>IMPORT</var> as <em>overnride referrer source</em> and <em>block cookies</em> flag set:
    <li><ol>
        <li>If fetched <a href="http://fetch.spec.whatwg.org/#concept-response-type">response type</a> is <i>error</i>:</li>
        <li><ul>
          <li>Add <var>LOCATION</var> and <strong>null</strong> to the <a href="#dfn-import-map">import map</a> and <strong>stop</strong>.
        </ul></li>
        <li>Let <var>IMPORT</var> be a new <a href="http://dom.spec.whatwg.org/#document"><code>Document</code></a>, <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#the-document's-address">the document's address</a> of which is <var>LOCATION</var></li>
        <li>Set <var>IMPORT</var> <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#no-quirks-mode">no quirks mode</a>.</li>
        <li>Let <var>PARSER</var> be a new <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#html-parser">HTML parser</a>, associated with <var>IMPORT</var></li>
        <li><div class="fixme">Input a <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#the-doctype">DOCTYPE</a> token to <var>PARSER</var>'s <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#tree-construction">tree construction stage</a> with name "html" and <i>force-quirks flag</i> turned <i>off</i>. <p>See <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=24349">Bug 24349</a>.</div>
        <li>Add <var>LOCATION</var> and <var>IMPORT</var> to the <a href="#dfn-import-map">import map</a>.</li>
        <li>For each <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#concept-task">task</a> that the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#concept-task">networking task source</a> places on the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#task-queue">task queue</a> while fetching:
        <ol>
            <li>Fill <var>PARSER</var>'s <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#the-input-byte-stream">input byte stream</a> with the fetched bytes</li>
            <li>Let <var>PARSER</var> process the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#the-input-byte-stream">input byte stream</a> with <a href="http://encoding.spec.whatwg.org/#utf-8">utf-8</a> as <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#a-known-definite-encoding">a known definite encoding</a></li>
        </ol></li>
        <li>When no more bytes are available:
        <ol>
            <li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#queue-a-task">Queue a task</a> from the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#networking-task-source">networking task source</a> for <var>PARSER</var> to process implied <code>EOF</code> character</li>
        </ol>
    </ol></li>
</ol>
</div>

<div class="informative">
All of loaded imports and imports under loading are in the <a href="#dfn-import-link-list">import link list</a>, thus every <a href="#dfn-import">import</a> which is linked from imports in the list will also be loaded using the <a href="#dfn-import-fetching">import fetching</a> algorithm, with  <var>LOCATION</var> be the <a href="#dfn-import-location">import location</a> of the import.
</div>

<p>
The loading attempt <strong>must</strong> be considered successful if <var>IMPORT</var> is not null on the algorithm completion, and failed otherwise.
</p>

<div class="informative">
The <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element"><code>link</code></a> element fires a simple event called <code>load</code>
for successful loading attempt. For failed attempt, it fires a simple event named <code>error</code>.
</div>

<h3 id="imports-and-csp">Imports and Content Security Policy</h3>

<p>
<a href="http://www.w3.org/TR/2014/WD-CSP11-20140211/">Content Security Policy</a> <strong>must</strong> restrict import loading through the <a href="http://www.w3.org/TR/2014/WD-CSP11-20140211/#script-src">script-src</a> directive.
</p>

<p>
Each import <strong>must</strong> be restricted by the Content Security Policy of the <a href="#dfn-master-document">master document</a>.
For example, if <a href="http://www.w3.org/TR/2014/WD-CSP11-20140211/#content-security-policy-header-field">Content Security Header Field</a> is sent to an import, the user agent must <a href="http://www.w3.org/TR/2014/WD-CSP11-20140211/#enforce">enforce</a> the policy of the <a href="#dfn-master-document">master document</a> to the imported document.
</p>

<h2 id="parsing-imports">Parsing Imports</h2>

<p>Parsing behaviour of <a href="#dfn-import">imports</a> is defined as a set of changes to the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#parsing">HTML Parsing</a>.</p>

<h3 id="additions-to-prepare-a-script-algorithm">Additions to Prepare A Script Algorithm</h3>

<p>In step 15 of <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#prepare-a-script">prepare a script</a> algorithm, modify the last part of condition which begins with <em>If element does not have a <code>src</code> attribute</em> to read:</p>
<div class="monkeypatch">
<p>... and the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document"><code>Document</code></a> of the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#html-parser">HTML parser</a> or <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-xhtml-syntax.html#xml-parser">XML parser</a> that created the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#the-script-element"><code>script</code></a> element <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#has-a-style-sheet-that-is-blocking-scripts">has a style sheet that is blocking scripts</a> or <a href="#dfn-has-an-import-that-is-blocking-scripts">has an import that is blocking scripts</a></p>
</div>

<h3 id="additions-to-tree-construction-algorithm">Additions to Tree Construction Algorithm</h3>

<p>In sub-condition named <em>Otherwise</em> of condition <em>An end tag whose name is "script"</em> in <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#parsing-main-incdata">"text" insertion mode</a>, modify step 3 to read:</p>
<div class="monkeypatch">
<ol start="3">
    <li>If the parser's <code><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document">Document</a></code> <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#has-a-style-sheet-that-is-blocking-scripts">has a style sheet that is blocking scripts</a> or <a href="#dfn-has-an-import-that-is-blocking-scripts">has an import that is blocking scripts</a> or <em>the script</em>'s <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#ready-to-be-parser-executed">"ready to be parser-executed"</a> flag is not set: <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#spin-the-event-loop">spin the event loop</a> until the parser's <code><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document">Document</a></code> <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#has-no-style-sheet-that-is-blocking-scripts">has no style sheet that is blocking scripts</a> and <a href="#dfn-has-no-import-that-is-blocking-scripts">has no import that is blocking scripts</a> and the script's <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#ready-to-be-parser-executed">"ready to be parser-executed"</a> flag is set.</li>
</ol>
</div>

<p>Modify sub-step 1 of step 3 of <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#the-end">the end</a> to read:</p>
<div class="monkeypatch">
<ol>
    <li><p><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#spin-the-event-loop">Spin the event loop</a> until the first <code><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#the-script-element">script</a></code> in the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#list-of-scripts-that-will-execute-when-the-document-has-finished-parsing">list of scripts that will execute when the <code>Document</code> has finished parsing</a> has its <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#ready-to-be-parser-executed">"ready to be parser-executed"</a> flag set <em>and</em> the parser's <code><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document">Document</a></code> <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#has-no-style-sheet-that-is-blocking-scripts">has no style sheet that is blocking scripts</a> <em>and</em> has <a href="#dfn-has-no-import-that-is-blocking-scripts">has no import that is blocking scripts</a>.</p></li>    
</ol>
</div>

<div class="fixme">
<p>All <a href="#dfn-import-dependent">import dependents</a> <strong>must</strong> be loaded before <code><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/section-index.html#event-domcontentloaded">DOMContentLoaded</a></code> is fired. See <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=23526">Bug 23526</a>.</p>
</div>

<h3 id="additions-to-parsing-xhtml-documents">Additions to Parsing XHTML Documents</h3>

<p>Modify step 3 of steps that run following <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#prepare-a-script">preparing</a> the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#the-script-element"><code>script</code></a> element to read:</p>
<div class="monkeypatch">
<ol start="3">
    <li><p><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#spin-the-event-loop">Spin the event loop</a> until the parser's <code><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document">Document</a></code> <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#has-no-style-sheet-that-is-blocking-scripts">has no style sheet that is blocking scripts</a> and <a href="#dfn-has-no-import-that-is-blocking-scripts">has no import that is blocking scripts</a> and the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#pending-parsing-blocking-script">pending parsing-blocking script</a>'s <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#ready-to-be-parser-executed">"ready to be parser-executed"</a> flag is set.</p></li>
</ol>
</div>

<div class="fixme">
<h3 id="additions-to-document-write">Additions to document.write() method</h3>

<p>Insert following step between step 2 and step 3 of <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#document.write()">document.write() method</a>:</p>

<div class="monkeypatch">
<ul>
  <li>If <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-document-currentscript">document.currentScript</a> points the element that isn't owned by the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#active-document">active document</a>, then abort these steps.</li>
</ul>
</div>

<p>See <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=24042">Bug 24042</a>.</p>
</div>

<h2 id="scripts-imports">Scripting in Imports</h2>

<h3 id="additions-to-script-enabling-criteria">Additions to Script Enabling Criteria</h3>

<p>Add following condition to the list of <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#enabling-and-disabling-scripting">Enabling and disabling scripting</a> criteria:</p>

<div class="monkeypatch">
<ul>
  <li>Scripting is enabled for a <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/infrastructure.html#node">node</a> if the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document">Document</a> object of the node is in the <a href="#dfn-import-map">import map</a>.</li>
</ul>
</div>

<h3 id="additions-to-dom-document-currentscript">Additions to document.currentScript</h3>

<p>
Modify the definition of <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-document-currentscript"><code>document.currentScript</code></a>
as follows:
</p>
<div class="monkeypatch">
The <dfn id="dom-document-currentscript" title="dom-document-currentScript"><code>currentScript</code></dfn> attribute, on getting,
must return the value to which it was most recently initialized in the document or the <a href="#dfn-import-map">import map</a> of the document.
When the Document is created, the <code>currentScript</code> must be initialized to null.
If the Document is an <a href="#dfn-imported-document">imported document</a>, its <code>currentScript</code> is always null.
</div>

<h2 id="style-imports">Style processing with Imports</h2>

<p>The contents of the <code><a href="http://www.whatwg.org/specs/web-apps/current-work/#the-style-element">style</a></code> elements and
the external resources of the <code><a href="http://www.whatwg.org/specs/web-apps/current-work/#the-link-element">link</a></code> elements in <a href="#dfn-import">imports</a> <strong>must</strong> be considered as input sources of the <a href="http://www.w3.org/TR/CSS21/intro.html#processing-model">style processing model</a> of the <a href="#dfn-master-document">master document</a>.</p>

<div class="fixme">
<p>Between declarations from different documents, the document order in terms of <a href="http://www.w3.org/TR/css3-cascade/#cascade-order">order of appearance</a> is defined based on the document order of the <code><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-link-element">link</a></code> elements of their <a href="#dfn-import-referrer-ancestor">import referrer ancestors</a> which are in the same document. If thre are more than one of such documents, the comparison result in the first document, in the document order, wins.</p>
<p>See <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=24756">Bug 24756</a>.</p>
</div>

<h2 id="events-imports">Events in Imports</h2>

<p>Events in <a href="#dfn-import">imports</a> is defined as a set of changes to the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#events">HTML Events</a>.</p>

<h3 id="additions-to-event-handlers">Additions to Event Handlers</h3>

<p>
Modify the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#event-handler-content-attributes">event handler content attribute</a>'s
script creation criteria by expanding the first paragraph:

<div class="monkeypatch">
<p>When an event handler content attribute is set, if the element is owned by a <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#document">Document</a> that is in a <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#browsing-context">browsing context</a> or in an <a href="#dfn-import-map">import map</a>, ...</p>
</div>

<h2 id="acknowledgements">Acknowledgements</h2>

<p><span class="vcard">David Hyatt</span> developed <a href="http://dev.w3.org/2006/xbl2/">XBL 1.0</a>, and <span class="vcard">Ian Hickson</span> co-wrote <a href="http://dev.w3.org/2006/xbl2/">XBL 2.0</a>. These documents provided tremendous insight into the problem of behavior attachment and greatly influenced this specification.</p>

<p><span class="vcard">Alex Russell</span> and his considerable forethought triggered a new wave of enthusiasm around the subject of behavior attachment and how it can be applied practically on the Web.</p>

<p><span class="vcard">Dominic Cooney</span> and <span class="vcard">Roland Steiner</span> worked tirelessly to scope the problem within the confines of the Web platform and provided a solid foundation for this document.</p>

<p>The editor would also like to thank <span class="vcard">Alex Komoroske</span>, <span class="vcard">Angelina Fabbro</span>, <span class="vcard">Anne van Kesteren</span>, <span class="vcard">Boris Zbarsky</span>, <span class="vcard">Brian Kardell</span>, <span class="vcard">Daniel Buchner</span>, <span class="vcard">Edward O'Connor</span>, <span class="vcard">Eric Bidelman</span>, <span class="vcard">Erik Arvidsson</span>, <span class="vcard">Elliott Sprehn</span>, <span class="vcard">Gabor Krizsanits</span>, <span class="vcard">Hayato Ito</span>, <span class="vcard">James Simonsen</span>, <span class="vcard">Jonas Sicking</span>, <span class="vcard">Ken Shirriff</span>, <span class="vcard">Neel Goyal</span>, <span class="vcard">Olli Pettay</span>, <span class="vcard">Rafael Weinstein</span>, <span class="vcard">Scott Miles</span>, <span class="vcard">Steve Orvell</span>, <span class="vcard">Tab Atkins</span>, <span class="vcard">William Chan</span>, and <span class="vcard">William Chen</span> for their comments and contributions to this specification.</p>

<p>This list is too short. There's a lot of work left to do. Please contribute by reviewing and filing bugs&mdash;and don't forget to ask the editor to add your name into this section.</p>

</section>
</body>
</html>
